package co.fitstart.admin.web.controller.trade;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import co.fitstart.admin.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;

import co.fitstart.admin.service.DataService;
import co.fitstart.entity.course.Course;

/**
 * 
 * @author jiecheng
 *
 */
@Controller
@RequestMapping("/courses")
@MenuSetting("menu-ecommerce")
public class CourseController {
    
    private static final Logger logger = LoggerFactory.getLogger(CourseController.class);
    
    @Autowired
    @Qualifier("dataService")
    private DataService<Course> dataService;

    /**
     * 
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-course-management")
    public ModelAndView list(){
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/course/course-list");
        
        List<Course> courses = dataService.loadAll(Course.class);
        
        modelAndView.addObject("courses", courses);
        
        return modelAndView;
    }
    
    /**
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @MenuSetting("submenu-course-management")
    public ModelAndView view(@PathVariable("id") Long id){
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/course/course-view");
        
        Course course = dataService.load(Course.class, id);
        
        modelAndView.addObject("course", course);
        
        return modelAndView;
    }
    
    /**
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("id") Long id){
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/course/course-edit");
        
        Course course = dataService.load(Course.class, id);
        
        modelAndView.addObject("course", course);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param course
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
    public @ResponseBody String onEdit(@Preloading("courseId") Course course){
        
        Result result = new Result();
        
        try {
            
            dataService.update(course);
            
            result.success().message("修改课程成功");
            
        }catch(Exception e){
            
            logger.error("#onEdit() - ", e);
            
            result.fail().message("修改链接失败");
        
        }
        
        return JsonUtils.toJsonString(result);
    }
    

    /**
     * Display edit cover panel
     * 
     * @param courseId 
     * @return
     */
    @RequestMapping(value = "/{courseId}/cover", method = RequestMethod.GET)
    public ModelAndView editCover(@PathVariable("courseId") Long courseId){
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/course/course-cover-edit");
        
        Course course = dataService.load(Course.class, courseId);
        modelAndView.addObject("course", course);
        
        return modelAndView;
    }
    
    /**
     * Do edit cover
     * 
     * @param courseId 
     * @param cover
     * @return
     */
    @RequestMapping(value = "/{courseId}/cover", method = RequestMethod.PATCH)
    public @ResponseBody String onEditCover(@PathVariable("courseId") Long courseId,
                                            @RequestParam("cover") String cover){
        
        Result result = new Result();
        
        try {
            
            Course course = dataService.load(Course.class, courseId);
            
            dataService.update(course.editCover(cover));
            
            result.success().message("修改课程封面成功！");
            
        } catch(Exception e) {
            
            logger.error("#onEditCover - ", e);
            
            result.fail().message("修改课程封面失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/appoint-time", method = RequestMethod.GET)
    public ModelAndView editAppointTime(@PathVariable("id") Long id){
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/course/course-appoint-time-edit");
        
        Course course = dataService.load(Course.class, id);
        
        modelAndView.addObject("course", course);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param course
     * @return
     */
    @RequestMapping(value = "/{id}/appoint-time", method = RequestMethod.PATCH)
    public @ResponseBody String onEditAppointTime(@Preloading("courseId") Course course){
        
        Result result = new Result();
        
        try {
            
            dataService.update(course);
            
            result.success().message("修改课程预约时间成功");
            
        }catch(Exception e){
            
            logger.error("#onEdit() - ", e);
            
            result.fail().message("修改预约时间失败");
        
        }
        
        return JsonUtils.toJsonString(result);
    }
    
}
